package com.alter.global.base;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.WebRequest;

import com.alter.global.base.article.service.ArticleCenterService;
import com.alter.global.base.personel.service.PersonalbaseService;
import com.alter.model.AfPhotoAlbum;
import com.alter.model.SeUser;
import com.alter.system.service.SysbaseService;

/**
 * 个人中心
 * 
 * @author a_alter
 */
@Controller
@RequestMapping("/personal")
public class PersonelAction {

    /**
     * 个人中心每页显示条数。
     */
    private static final int PERSONEL_ARTICLE_PAGE_SIZE = 10;

    @Autowired
    private PersonalbaseService baseService;

    @Autowired
    private SysbaseService sysbaseservice;

    @Autowired
    private ArticleCenterService articleService;

    /**
     * 个人中心首页
     * 
     * @return
     */
    @RequestMapping("/index")
    public String index() {
	return "/personal/index";
    }

    /**
     * 当前登录人员相册
     * 
     * @param userid
     *            用户id如果为空的话查看当前登录用户ID。
     * @param user
     *            系统当前用户。
     */
    @RequestMapping("/album")
    public String photoAlbum(@ModelAttribute("user") SeUser user, @RequestParam(value = "user", required = false) String userid,
	    Model model) {
	return "/personal/photoalbum";
    }

    /**
     * 创建个人相册。
     */
    @RequestMapping(value = "/photo/create")
    public String createPhotoAlbum(@ModelAttribute AfPhotoAlbum album, WebRequest request) {
	SeUser user = (SeUser) request.getAttribute("user", RequestAttributes.SCOPE_SESSION);
	album.setUser(user);
	album.setCreater(user.getName());
	album.setCreate_date(Calendar.getInstance().getTime());
	baseService.savePhotoAlbum(album);
	return "redirect:/personal/" + user.getName() + "/album";
    }

    /**
     * 个人信息管理
     * 
     * @param type
     *            <ul>
     *            <li>admin: 个人信息配置</li>
     *            <li>blog: 博客配置</li>
     *            </ul>
     * 
     * @param request
     *            封装后的request信息。
     */
    @RequestMapping(value = "/admin/{type}")
    public String blogadmin(@PathVariable String type, WebRequest request, Model model) {
	/* 是否找到匹配 */
	boolean bl = false;
	if ("blog".equals(type)) {
	    bl = true;
	} else {
	    if (!bl) {
		type = "admin";
	    }
	}
	model.addAttribute("tabType", type);
	return "/personal/console";
    }

    /**
     * 个人博文。
     * 
     * @return
     */
    @RequestMapping("/{name}/blog")
    public String blog(@ModelAttribute ArticleSearchVO searchVO, @PathVariable String name, Model model, WebRequest request) {
	SeUser user = (SeUser) request.getAttribute("user", RequestAttributes.SCOPE_SESSION);
	searchVO.setPageSize(PERSONEL_ARTICLE_PAGE_SIZE);
	model.addAttribute("list", articleService.find(searchVO));
	model.addAttribute("typeList", articleService.searchAriticleTypesByUser(user));
	return "/personal/article";
    }

    /**
     * 个人主页 因为是匹配模式，改方法放在Class 最末尾。
     * 
     * @return
     */
    @RequestMapping("/{name}/{type}")
    public String main(@PathVariable String name, @PathVariable String type, Model model) {
	SeUser user = sysbaseservice.searchSeUserByName(name);
	/* 找到匹配信息 */
	boolean findBl = false;
	if (type.equals("album")) {
	    List<AfPhotoAlbum> list = baseService.searchPhotoAlbumByUserAid(user.getAid());
	    model.addAttribute("albumList", list);
	    findBl = true;
	}
	// else if(){
	//
	// }
	if (!findBl) {
	    type = "main"; // default
	}
	model.addAttribute("type", type);
	return "/personal/main";
    }

}
